package waal.pc.backend.writer;

import java.util.HashMap;
import java.util.Map;

import waal.pc.backend.Log;
import waal.pc.backend.hub.Hub;
import waal.pc.backend.hub.HubEvent;
import waal.pc.backend.hub.HubEventListener;
import waal.pc.frontend.Surface;




public class CSVwriter implements HubEventListener {

	private Surface surface;
	private Map<Hub, CSVwriterHub> activeHubs;
	
	public CSVwriter(Surface surface) {
		this.surface = surface;
		this.surface.addHubEventListener(this);
		this.activeHubs = new HashMap<Hub, CSVwriterHub>();
	}
	
	@Override
	public void actionHubEvent(HubEvent e) {
		switch(e.getEvent()) {
		case 3:
			//this.startHub(e.getHub());
			break;
		case 4: 
			this.stopHub(e.getHub());		
			break;
		}
	}

	public void startHub(Hub hub) {
		Log.addToDebug(1, "CSVWriter", "startHub", "Start CSV writer for hub " + hub.getNumber());
		CSVwriterHub w = new CSVwriterHub(hub);
		this.activeHubs.put(hub, w);
		w.start();
	}
	
	private void stopHub(Hub hub) {
		if(this.activeHubs.containsKey(hub)) {
			this.activeHubs.remove(hub).stopWriter();
		}
	}
	
}
